Skip to content

Remove integer indexing from game collections in pygambit#942

Open
d-kad wants to merge 20 commits into
gambitproject:masterfrom
d-kad:indexing
Open

Remove integer indexing from game collections in pygambit#942
d-kad wants to merge 20 commits into
gambitproject:masterfrom
d-kad:indexing

Conversation

@d-kad

@d-kad d-kad commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Description

Game object collections in pygambit are now indexed by string label only.

  • Integer indexing is removed from Game.players, Game.outcomes, Game.strategies, Game.infosets,
    Game.actions, Player.strategies, Player.infosets, Player.actions, Infoset.actions, Infoset.members,
    and Node.children. Indexing by an integer raises TypeError with a migration message.
    Positional access remains available by iterating a collection (e.g. pl1, pl2 = game.players).
    Indexing a Game by a contingency (e.g. game[0, 1]) is unchanged.
  • Label lookup continues to strip leading/trailing whitespace from the lookup string before comparing
    (existing behavior, now centralised and pinned by a test).
    A label matching no object uniformly raises KeyError (Node.children previously raised ValueError).
  • Lookup is delegated to a new _resolve_by_label helper; all the collection __getitem__ methods use it.
    The label parameter is deliberately unannotated so the migration TypeError is reachable
    (a str annotation would compile to an enforced check in Cython).
  • The test suite (including the tutorial notebooks) is fully migrated to label-based access (~460 affected tests),
    with new tests added pinning the integer-rejection message and exact-match lookup across all collections
    (COLLECTION_GETTERS in tests/test_game.py).

d-kad added 19 commits June 9, 2026 12:40
iteration and tuple unpacking in quickstart, stripped-down poker, and
OpenSpiel tutorials.
Players, outcomes, strategies, infosets, actions, infoset
members and Node.children  are indexed by str label only.
Integer indexing raises TypeError with a 16.7.0 migration message;
unknown labels raise KeyError; lookup is by exact match, with no
whitespace stripping.  Contingency indexing on Game is unchanged.
@d-kad d-kad requested review from rahulsavani and tturocy June 12, 2026 07:04
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@d-kad

d-kad commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Design question — should label lookup strip whitespace, or match exactly?

Before this PR, the collection __getitem__s stripped the lookup string before comparing:

matches = [x for x in self if x.label == index.strip()]

This appears at 10 sites (game.pxi ×5, player.pxi ×3, infoset.pxi ×2),
so e.g. g.players[" Alice "] would find the player labelled "Alice".
The most recent new Node.children semantics in #588, matches labels verbatim.

In this PR I matched labels exactly. ~~ (superseded — see update)

UPD: @rahulsavani and I agreed label lookup should keep stripping surrounding
whitespace from the query (stored labels are matched as-is).

@d-kad d-kad self-assigned this Jun 12, 2026
@d-kad d-kad added python Items which involve coding in Python enhancement cython Items which involve coding in Cython labels Jun 12, 2026
@d-kad d-kad added this to the gambit-16.7.0 milestone Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cython Items which involve coding in Cython enhancement python Items which involve coding in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant